#ifndef __UART_H_
#define __UART_H_
#include "bsp.h"
#include "lpc177x_8x_uart.h"



/* buffer size definition */
#define UART_RING_BUFSIZE 4096
/* Buf mask */
#define __BUF_MASK (UART_RING_BUFSIZE-1)		 //__BUF_MASK=0xFF
/* Check buf is full or not */
#define __BUF_IS_FULL(head, tail) ((tail&__BUF_MASK)==((head+1)&__BUF_MASK))
/* Check buf will be full in next receiving or not */
#define __BUF_WILL_FULL(head, tail) ((tail&__BUF_MASK)==((head+2)&__BUF_MASK))
/* Check buf is empty */
#define __BUF_IS_EMPTY(head, tail) ((head&__BUF_MASK)==(tail&__BUF_MASK))
/* Reset buf */
#define __BUF_RESET(bufidx)	(bufidx=0)
#define __BUF_INCR(bufidx)	(bufidx=(bufidx+1)&__BUF_MASK)

extern __IO FlagStatus TxIntStat;

/** @brief UART Ring buffer structure */
typedef struct
{
//    __IO uint32_t tx_head;                /*!< UART Tx ring buffer head index */
//    __IO uint32_t tx_tail;                /*!< UART Tx ring buffer tail index */
    __IO uint32_t rx_head;                /*!< UART Rx ring buffer head index */
    __IO uint32_t rx_tail;                /*!< UART Rx ring buffer tail index */
//    __IO uint8_t  tx[UART_RING_BUFSIZE];  /*!< UART Tx data ring buffer */
    __IO uint8_t  rx[UART_RING_BUFSIZE];  /*!< UART Rx data ring buffer */
} UART_RING_BUFFER_T;

extern UART_RING_BUFFER_T *rb0,*rb1,*rb2,*rb3,*rb4;
void Init_Uart(uint8_t Uart_NO,uint32_t Baud_Rate);
void Uart_Sendstring(uint8_t UartNum,uint8_t *sendstring);
void Uart485_Sendstring(uint8_t *sendstring,uint32_t buflen);

void Uart485_2_Sendstring(uint8_t *sendstring,uint32_t buflen);

uint32_t UARTReceived(UART_ID_Type UARTx, uint8_t *rxbuf, uint32_t buflen);
void Uart_Sendchar(uint8_t UartNum,uint8_t sendchar);

#endif
